//-----------------------------------------------------------------------
// <copyright file="a_VoteLogDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_VoteLogDAOBase
    {
        #region Common methods
        public virtual a_VoteLog Createa_VoteLogFromReader(IDataReader reader)
        {
            a_VoteLog item = new a_VoteLog();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("VID"))) item.VID = (int)reader["VID"];
                if (!reader.IsDBNull(reader.GetOrdinal("RID"))) item.RID = (int)reader["RID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CookieID"))) item.CookieID = (string)reader["CookieID"];
                if (!reader.IsDBNull(reader.GetOrdinal("QID_Comment"))) item.QID_Comment = (int)reader["QID_Comment"];
                if (!reader.IsDBNull(reader.GetOrdinal("VoteDate"))) item.VoteDate = (DateTime)reader["VoteDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("IP"))) item.IP = (string)reader["IP"];
                if (!reader.IsDBNull(reader.GetOrdinal("OptionID"))) item.OptionID = (int)reader["OptionID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_VoteLog methods
            
        public virtual void Createa_VoteLog(a_VoteLog a_VoteLog)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_VoteLog_ins");
                
                database.AddInParameter(dbCommand, "@RID", DbType.Int32, a_VoteLog.RID);
                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, a_VoteLog.CookieID);
                database.AddInParameter(dbCommand, "@QID_Comment", DbType.Int32, a_VoteLog.QID_Comment);
                database.AddInParameter(dbCommand, "@VoteDate", DbType.DateTime, a_VoteLog.VoteDate);
                database.AddInParameter(dbCommand, "@IP", DbType.AnsiString, a_VoteLog.IP);
                database.AddInParameter(dbCommand, "@OptionID", DbType.Int32, a_VoteLog.OptionID);
                database.AddOutParameter(dbCommand, "@VID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_VoteLog.VID = (int)database.GetParameterValue(dbCommand, "@VID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_VoteLog methods
        
        public virtual void Updatea_VoteLog(a_VoteLog a_VoteLog)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_VoteLog_upd");            
                
                database.AddInParameter(dbCommand, "@VID", DbType.Int32, a_VoteLog.VID);
                database.AddInParameter(dbCommand, "@RID", DbType.Int32, a_VoteLog.RID);
                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, a_VoteLog.CookieID);
                database.AddInParameter(dbCommand, "@QID_Comment", DbType.Int32, a_VoteLog.QID_Comment);
                database.AddInParameter(dbCommand, "@VoteDate", DbType.DateTime, a_VoteLog.VoteDate);
                database.AddInParameter(dbCommand, "@IP", DbType.AnsiString, a_VoteLog.IP);
                database.AddInParameter(dbCommand, "@OptionID", DbType.Int32, a_VoteLog.OptionID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_VoteLog methods
        public virtual void Deletea_VoteLog(int vid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_VoteLog_del");
                
                database.AddInParameter(dbCommand, "@VID", DbType.Int32, vid);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_VoteLog methods
        
        public virtual a_VoteLog Geta_VoteLog(int vid)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_VoteLog_sel");
                
                database.AddInParameter(dbCommand, "@VID", DbType.Int32, vid);
                
                a_VoteLog a_VoteLog =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_VoteLog = Createa_VoteLogFromReader(reader);
                        reader.Close();
                    }
                }
                return a_VoteLog;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_VoteLogList methods
        public virtual a_VoteLogCollection Geta_VoteLogList(a_VoteLogColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_VoteLog_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_VoteLogCollection a_VoteLogCollection = new a_VoteLogCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_VoteLog a_VoteLog = Createa_VoteLogFromReader(reader);
                        a_VoteLogCollection.Add(a_VoteLog);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_VoteLogCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_VoteLogCollection Geta_VoteLogList(a_VoteLogColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_VoteLogList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}